package com.tansun.rule.sessionPool;

import java.io.File;

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.kie.api.KieBase;
import org.kie.api.KieBaseConfiguration;
import org.kie.api.KieServices;
import org.kie.api.builder.KieRepository;
import org.kie.api.builder.ReleaseId;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

import com.tansun.rule.config.AppConfiguration;
import com.tansun.rule.exception.RuleDeployException;
import com.tansun.rule.pojo.RulePackageMeta;
import com.tansun.rule.utils.DeployUtils;
/**
 * 
 * @author WY
 * 用于创建规则链接session
 */
public class PooledSessionFactory extends BasePooledObjectFactory<KieSession> {

	
	private  KieServices kieServices;
	private  KieContainer kieContainer;
	private  KieRepository kieRepository;
	private  KieBase kieBase;
	//private  KieSessionConfiguration ksconf;
	//private  Environment env;
	
	/**contractor*/
	public PooledSessionFactory() {
	}
	/**contractor*/
	public PooledSessionFactory(File file, String releaseGroupId, String releaseArtifactId, String releaseVersion, String kieBaseName) {
		init(file, releaseGroupId, releaseArtifactId, releaseVersion, kieBaseName);
	}
	

	
	/**
	 * 创建session对象工厂
	 * @param ruleProject
	 * @return
	 * @throws RuleDeployException
	 */
	public static PooledSessionFactory buildPooledSessionFactoryByRuleProj(String ruleProject) throws RuleDeployException{
		//if(new File(AppConfiguration.APPHOMEDIR+""+AppConfiguration.RuleProjDeployDir).exists() == false){
		if(new File(AppConfiguration.RuleProjDeployDir).exists() == false){
			throw new RuleDeployException("部署不存在！");
		}
		//规则包
		//String jarfilepath = AppConfiguration.APPHOMEDIR+""+AppConfiguration.RuleProjDeployDir+"/"+ruleProject+"/"+AppConfiguration.JarFile;
		String jarfilepath = AppConfiguration.RuleProjDeployDir+"/"+ruleProject+"/"+AppConfiguration.JarFile;
		
		RulePackageMeta rpm =  DeployUtils.readMavenPomProp(jarfilepath);
		
		DeployUtils.insertSessionNamefromKmodule(rpm, jarfilepath);
		
		String releaseGroupId = rpm.getGroupId();
		String releaseArtifactId = rpm.getArtifactId();
		String releaseVersion = rpm.getVersion();
		String kieBaseName = rpm.getKbaseName();
		File file = new File(jarfilepath);
		
		PooledSessionFactory psf = new PooledSessionFactory();
		psf.init(file, releaseGroupId, releaseArtifactId, releaseVersion, kieBaseName);
		return psf;
	}
	public void init(File file, String releaseGroupId, String releaseArtifactId, String releaseVersion, String kieBaseName) {

		kieServices = KieServices.Factory.get();
		kieRepository = kieServices.getRepository(); 
		kieRepository.addKieModule(kieServices.getResources().newFileSystemResource(file));
//		FileInputStream fis=null;
//		try {
//			fis = new FileInputStream(file);
//			kieRepository.addKieModule(kieServices.getResources().newInputStreamResource(fis, "UTF-8"));
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}finally{
//			try {
//				fis.close();
//				fis=null;
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
		//kieRepository.addKieModule(kieServices.getResources().newFileSystemResource(file));
		ReleaseId releaseId = kieServices.newReleaseId(releaseGroupId, releaseArtifactId, releaseVersion);
		kieContainer = kieServices.newKieContainer(releaseId);
		KieBaseConfiguration kbconfig = KieServices.Factory.get().newKieBaseConfiguration();
		kieBase = kieContainer.newKieBase(kieBaseName, kbconfig);
		//env = kieServices.newEnvironment();
		
		//ksconf = KieServices.Factory.get().newKieSessionConfiguration();
		
	}
	/**
	 * 新建对象
	 */
//	@Override
//	public KieSession create() throws Exception {
//		KieSession kSession = kieBase.newKieSession(KieServices.Factory.get().newKieSessionConfiguration(), kieServices.newEnvironment());
//		return kSession;
//	}
	/**
	 * 获取对象
	 */
	@Override
	public PooledObject<KieSession> wrap(KieSession session) {
		return new DefaultPooledObject<KieSession>(session);
	}
	/**
	 * 对象池对象项销毁
	 */
	@Override
	public void destroyObject(PooledObject<KieSession> p) throws Exception {
		p.getObject().destroy();
		super.destroyObject(p);
	}
	
	/**
	 * 新建对象
	 */
	public KieSession create(){
		KieSession kSession = kieBase.newKieSession();
		return kSession;
	}

}
